<?php
/*
 * Created on Sep 13, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("./configure/configure.php");		//--> global var

include_once("Smarty.class.php");				//--> out template
include_once("Common.php");

include_once("Strings.php");					//--> String utils
include_once("UploadFiles.php");				//--> UploadFiles
include_once("Zone.php");						//--> Zone
include_once("customerSession.inc.php");
include_once("Session.php");					//-- Session
include_once("orders/Cart.php");					//--> Cart
include_once("orders/CartProduct.php");				//--> Cart
include_once("orders/CartProductAttribute.php");	//--> Cart


session_start();


$common 	= new Common();
$objStrings = new Strings();
$objZone	= new Zone(0);										//--> Zone

$objCart 	= new Cart();		//--> Cart
$objSession	= new Session(DB_TAG_SYSTEM, SESSION_TABLE_NAME);	//-- session

/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir 	= TEMPLATE_DIR;
$smarty->compile_dir  	= COMPILE_DIR;
$smarty->cache_dir  	= CACHE_DIR;

$smarty->caching 		= CACHING;
$smarty->cache_lifetime = CACHE_LIFETIME;

if($_SESSION['cart']){
	$objCart = unserialize($_SESSION['cart']);
}

$isLogin	= false;

if($objSession->exist()) {
	$isLogin	= true;
}

if(isset($_POST['submit']) && $_POST['submit'] == 'Delete'){
	$prodIndex = $_POST['prodIndex'];
	
	$objCart->removeProduct($prodIndex);
	
	$_SESSION['cart'] = serialize($objCart);	
}

if(isset($_POST['submit']) && $_POST['submit'] == 'Update'){
	$prodIndex 	= $_POST['prodIndex'];
	$quantity 	= $_POST['quantity'];
	
	if(is_numeric($quantity)){
		
		
		$product = $objCart->getProduct($prodIndex);
		
		
		$product->setQuantity($quantity);
	}
	
	$_SESSION['cart'] = serialize($objCart);	
}

/*--------------- coupon start ---------------*/
if(isset($_POST['coupon']) && $_POST['coupon'] != ''){
	$code = $_POST['coupon'];
	
	$oCouponImpl = new CouponImpl(DB_TAG_PUBLIC, 0);
	
	$oCoupon = $oCouponImpl->getCoupon(DB_TAG_PUBLIC, $code);
	
	if(is_object($oCoupon)){
		if($oCoupon->isExpire()){
			$objCart->setCoupon(null);
			
			$error_message = 'The promo code you entered is expired.';
		}else{
			$objCart->setCoupon($oCoupon);
			
			$SaveOff = $objCart->getSaveOff();
			
			if($SaveOff > 0)
				$error_message = 'SAVE OFF $' . $SaveOff / 100;
		}
	}else{
		$objCart->setCoupon(null);
		
		$error_message = 'The promo code you entered is invalid.';
	}
	
	$_SESSION['cart'] 	= serialize($objCart);
}


$zoneid = 12;
$tax 	= 0;
$arrTaxs= array();

/*--------------- calculate tax start ---------------*/
if(isset($_POST['zoneid']) && $_POST['zoneid'] != ''){
	$total = $objCart->getAmount();
	
	$zoneid = $_POST['zoneid'];
	
	$arrTax = $common->getHash(DB_TAG_PUBLIC, "SELECT tr.id, tr.rate FROM tax_class tc, tax_rates tr WHERE tc.id = tr.classID AND tr.zoneID = $zoneid ORDER BY priority");
	foreach($arrTax as $key => $value){
		$taxSubTotal = $value * $total;
		
		$arrTaxs[$key]	= round($taxSubTotal / 1000000, 2);
		
		$tax += $taxSubTotal;
	}
	
	if($tax > 0){
		$tax = round($tax / 1000000, 2);
		
		$error_tax = 'TOTAL TAX : $' . $tax;
	}else{
		$error_tax = 'TOTAL TAX : $0.00';
	}

	
}

$arrZones = $objZone->getGroupList(223);

include_once("includeCategory.php");	//--> include category
include_once("includeSpec.php");		//--> include spec



if(!$smarty->isCached('viewCart.html')){
	$smarty->assign('categorys', $categorys);
	$smarty->assign('topCategory', $topCategory);
	$smarty->assign('specProducts', $specProducts);
}

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL);

$smarty->assign('error_message', $error_message);
$smarty->assign('error_tax', $error_tax);

$smarty->assign('objCart', $objCart);
$smarty->assign('objStrings', $objStrings);

$smarty->assign('arrZones', $arrZones);
$smarty->assign('zoneid', $zoneid);

$smarty->assign('isLogin', $isLogin);

$smarty->display('viewCart.html');
?>
